/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  CustomDialogUtil.java   
 * @Package com.swift.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-7-6 下午3:37:05   
 * @version V1.0     
 */
package com.swift.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swift.R;

public class CustomDialogUtil {
	Context context;
	android.app.AlertDialog ad;
	TextView tvTitle;
	TextView tvContent;
	TextView tvCancle;
	TextView tvSure;
	
	LinearLayout buttonLayout;

	public CustomDialogUtil(Context context) {
		this.context = context;
		ad = new android.app.AlertDialog.Builder(context).create();
		ad.show();
		// 将对话框的大小按屏幕大小的百分比设置
		WindowManager windowManager = ((Activity)context).getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = ad.getWindow().getAttributes();
		lp.width = (int)(display.getWidth() * 0.8); //设置宽度
		lp.gravity = Gravity.CENTER;
		ad.getWindow().setAttributes(lp);
		
		// 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
		Window window = ad.getWindow();
		window.setContentView(R.layout.dialog_custom);
		tvTitle = (TextView) window.findViewById(R.id.tvTitle);
		tvContent = (TextView) window.findViewById(R.id.tvContent);
		tvCancle = (TextView) window.findViewById(R.id.tvCancle);
		tvSure = (TextView) window.findViewById(R.id.tvSure);
	}

	public void setTitle(int resId) {
		tvTitle.setText(resId);
	}

	public void setTitle(String title) {
		tvTitle.setText(title);
	}

	public void setMessage(int resId) {
		tvContent.setText(resId);
	}

	public void setMessage(String message) {
		tvContent.setText(message);
	}

	/**
	 * 设置按钮
	 * 
	 * @param text
	 * @param listener
	 */
	public void setPositiveButton(String text,
			final View.OnClickListener listener) {
		tvSure.setText(text);
		tvSure.setOnClickListener(listener);
	}

	/**
	 * 设置按钮
	 * 
	 * @param text
	 * @param listener
	 */
	public void setNegativeButton(String text,
			final View.OnClickListener listener) {
		tvCancle.setText(text);
		tvCancle.setOnClickListener(listener);
	}

	/**
	 * 关闭对话框
	 */
	public void dismiss() {
		ad.dismiss();
	}
	
	public Dialog getDialog()
	{
		return ad;
	}
}
